// zhouyuyue_yajin/pages/shoppingCart/shoppingCart.js
let app = getApp();
let http = require('../../util/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // cart_list:[{
    //   "format_week": "周三",
    //   "food": [{
    //     "id": "1",
    //     "name": "蔬菜1",
    //     "poster": "https:\/\/www.missweb.cn\/attachment\/images\/4\/2020\/08\/xa5KXiS1ssXAzCfjdHGsHSk13hsS5P.jpg",
    //     "sku": "11",
    //     "price": "21.50",
    //     "categoryName": "蔬菜",
    //     "count": 2, //购物车中的菜品数量
    //     "send_time":"2020-09-12"
    //   }, {
    //     "id": "2",
    //     "name": "蔬菜2",
    //     "poster": "https:\/\/www.missweb.cn\/attachment\/images\/4\/2020\/08\/xa5KXiS1ssXAzCfjdHGsHSk13hsS5P.jpg",
    //     "sku": "22",
    //     "price": "12.00",
    //     "categoryName": "蔬菜",
    //     "count": 1, //购物车中的菜品数量
    //     "send_time":"2020-09-12"
    //   }]
    // },{
    //   "format_week": "周二",
    //   "food": [{
    //     "id": "1",
    //     "name": "蔬菜1",
    //     "poster": "https:\/\/www.missweb.cn\/attachment\/images\/4\/2020\/08\/xa5KXiS1ssXAzCfjdHGsHSk13hsS5P.jpg",
    //     "sku": "11",
    //     "price": "21.50",
    //     "categoryName": "蔬菜",
    //     "count": 2 ,//购物车中的菜品数量
    //     "send_time":"2020-09-12"
    //   }, {
    //     "id": "2",
    //     "name": "蔬菜2",
    //     "poster": "https:\/\/www.missweb.cn\/attachment\/images\/4\/2020\/08\/xa5KXiS1ssXAzCfjdHGsHSk13hsS5P.jpg",
    //     "sku": "22",
    //     "price": "12.00",
    //     "categoryName": "蔬菜",
    //     "count": 1 ,//购物车中的菜品数量
    //     "send_time":"2020-09-12"
    //   }]
    // }],//购物车列表
    cart_list:[],
    is_all_sel:false,//是否全选
    all_num:0,//全部数量
    all_price:'0.00',//全部价格
    page_type:0,//0：选择商品 1：删除商品
    is_click:false,//按钮是否可点
  },

  //输入商品数量
  inputNum:function(e){
    // console.log(e.detail.value);
    let that = this;
    let i = e.currentTarget.dataset.i;
    let j = e.currentTarget.dataset.j;
    let week = that.weekTextToNum(e.currentTarget.dataset.week);
    if(e.detail.value!=''){
      http.post('cartGoodsCount',{
        goodsId:e.currentTarget.dataset.id,
        send_week:week,
        type:30,
        num:e.detail.value
      },function(res){
        // console.log('输入商品数量',res)
        that.getCartList();
      });
    }
  },

  //生成删除列表
  getDelList:function(){
    let that = this;
    let list = new Array();
    for(let i=0;i<that.data.cart_list.length;i++){
      list[i] = {};
      list[i].send_week = that.weekTextToNum(that.data.cart_list[i].format_week);
      list[i].goodsIds = new Array();
      for(let j=0;j<that.data.cart_list[i].food.length;j++){
        if(that.data.cart_list[i].food[j].choosed){
          list[i].goodsIds.push(that.data.cart_list[i].food[j].id);
        }
      }
    };
    return list;
  },

  //提交订单
  cartToOrder:function(){
    let that = this;
    wx.navigateTo({
      url: '/zhouyuyue_yajin/pages/placeOrder/placeOrder',
    })
  },

  //删除购物车
  deleteCart:function(){
    let that = this;
    wx.showModal({
      content: '是否确认删除该商品？',
      showCancel: true,
      title: '提示',
      success: (result) => {
        if(result.confirm){
          http.post('deleteCart',{
            option:JSON.stringify(that.getDelList())
          },function(res){
            // console.log('删除购物车',res);
            if(res.data.errno==0){
              that.getCartList();
              that.setData({
                is_all_sel:false,//是否全选
                all_num:0,//全部数量
                all_price:'0.00',//全部价格
                is_click:false,//按钮是否可点
              });
            }
          });
        }
      },
      fail: (res) => {},
      complete: (res) => {},
    })
    // console.log(that.getDelList());
  },

  //更改页面类型
  changePageType:function(){
    let that = this;
    for(let i=0;i<that.data.cart_list.length;i++){
      for(let j=0;j<that.data.cart_list[i].food.length;j++){
        that.data.cart_list[i].food[j].choosed = false
      }
    };
    that.setData({
      page_type:that.data.page_type==0?1:0,
      cart_list:that.data.cart_list,
      is_all_sel:false,
      is_click:false,
      all_num:0,
      all_price:'0.00'
    });
    that.getAllPrice();
  },

  weekTextToNum:function(text){
    if(text=='周一'){
      return 1;
    }
    if(text=='周二'){
      return 2;
    }
    if(text=='周三'){
      return 3;
    }
    if(text=='周四'){
      return 4;
    }
    if(text=='周五'){
      return 5;
    }
    if(text=='周六'){
      return 6;
    }
    if(text=='周日'){
      return 0;
    }
  },

  //更改数量
  changeNum:function(e){
    let that = this;
    let i = e.currentTarget.dataset.i;
    let j = e.currentTarget.dataset.j;
    let type = e.currentTarget.dataset.type;
    let week = that.weekTextToNum(e.currentTarget.dataset.week);
    if(type!=20 || that.data.cart_list[i].food[j].count>1){
      http.post('cartGoodsCount',{
        goodsId:e.currentTarget.dataset.id,
        send_week:week,
        type:type
      },function(res){
        console.log('增减商品数量',res);
        if(res.data.errno==0){
          if(type==10){
            that.data.cart_list[i].food[j].count = that.data.cart_list[i].food[j].count*1 + 1
          } else {
            that.data.cart_list[i].food[j].count = that.data.cart_list[i].food[j].count*1 - 1
          }
          that.setData({
            cart_list:that.data.cart_list
          });
          that.getAllPrice();
        }
      });
    }
  },

  //点击全选按钮
  allSel:function(){
    let that = this;
    that.data.is_all_sel = !that.data.is_all_sel;
    for(let i=0;i<that.data.cart_list.length;i++){
      for(let j=0;j<that.data.cart_list[i].food.length;j++){
        that.data.cart_list[i].food[j].choosed = that.data.is_all_sel
      }
    };
    that.setData({
      is_all_sel:that.data.is_all_sel,
      is_click:that.data.is_all_sel,
      cart_list:that.data.cart_list
    });
    that.getAllPrice();
  },

  //计算价格
  getAllPrice:function(){
    let that = this;
    let all_num = 0;
    let all_price = 0;
    // for(let i=0;i<that.data.cart_list.length;i++){
    //   for(let j=0;j<that.data.cart_list[i].food.length;j++){
    //     if(that.data.cart_list[i].food[j].choosed){
    //       all_num = all_num + that.data.cart_list[i].food[j].count*1;
    //       all_price = all_price + that.data.cart_list[i].food[j].price*that.data.cart_list[i].food[j].count;
    //     }
    //   }
    // };
    for(let i=0;i<that.data.cart_list.length;i++){
      for(let j=0;j<that.data.cart_list[i].food.length;j++){
        all_num = all_num + that.data.cart_list[i].food[j].count*1;
        all_price = all_price + that.data.cart_list[i].food[j].price*that.data.cart_list[i].food[j].count;
      }
    };
    that.setData({
      all_num:all_num,
      all_price:all_price.toFixed(2)
    });
  },

  //是否全选
  isAllSel:function(){
    let that = this;
    that.data.is_all_sel = true;
    that.data.is_click = false;
    for(let i=0;i<that.data.cart_list.length;i++){
      for(let j=0;j<that.data.cart_list[i].food.length;j++){
        if(!that.data.cart_list[i].food[j].choosed){
          that.data.is_all_sel = false;
        } else {
          that.data.is_click = true;
        }
      }
    };
    that.setData({
      is_all_sel:that.data.is_all_sel,
      is_click:that.data.is_click
    });
  },

  //选择商品
  chooseGoods:function(e){
    let that = this;
    // console.log(e);
    that.data.cart_list[e.currentTarget.dataset.i].food[e.currentTarget.dataset.j].choosed = !that.data.cart_list[e.currentTarget.dataset.i].food[e.currentTarget.dataset.j].choosed;
    that.setData({
      cart_list:that.data.cart_list
    });
    that.isAllSel();
    that.getAllPrice();
  },

  //获取购物车列表
  getCartList:function(){
    let that = this;
    http.get('cart',{},function(res){
      console.log('购物车列表',res);
      for(let i=0;i<res.data.data.length;i++){
        for(let j=0;j<res.data.data[i].food.length;j++){
          res.data.data[i].food[j].choosed = false
        }
      };
      that.setData({
        cart_list:res.data.data
      });
      that.getAllPrice();
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    that.setData({
      is_all_sel:false,
      all_num:0,//全部数量
      all_price:'0.00',//全部价格
      page_type:0,//0：选择商品 1：删除商品
      is_click:false,//按钮是否可点
    });
    that.getCartList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})